package com.chhuang.sync;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.chhuang.bean.OperateBean;
import com.chhuang.bean.QueryTermBean;
import com.chhuang.common.Assert;
import com.chhuang.common.DataOperateAbstract;

/**
 * 用户信息表定制同步线程
 * @author PrinceChen
 *
 */
public class FullQueryAddSyncThread extends DataOperateAbstract implements Runnable {
	private static final Log LOG = LogFactory.getLog(FullQueryAddSyncThread.class);
	
	protected String fromTable = null;
	protected String toTable = null;
	protected QueryTermBean initQueryTermBean = null;
	protected OperateBean operateBean = null;
	protected String[] selectColumns = null;
	
	/**
	 * 构造函数
	 * @param fromTable 来源表
	 * @param toTable 目的表
	 * @param initQueryTermBean 初始化查询条件（如：modifyTimeColumn>=?和 Timestamp(2016-01-01)）
	 * @param operateBean 操作控制（添加，更新，删除）
	 * @param selectColumns 需要查询字段，为空时表示查询所有字段
	 */
	public FullQueryAddSyncThread(String fromTable, String toTable, QueryTermBean initQueryTermBean, 
			OperateBean operateBean, String... selectColumns){
		this.fromTable = fromTable;
		this.toTable = toTable;
		this.initQueryTermBean = initQueryTermBean;
		this.operateBean = operateBean;
		this.selectColumns = selectColumns;
	}

	@Override
	public void run() {
		if(Assert.isInvalidString(fromTable) || Assert.isInvalidString(toTable)){
			return ;
		}
		boolean result = false;
		
		LOG.info("-----table ("+fromTable+"->"+toTable+") start sync-----");

		Connection toConn = c3p0Helper.getConnection(c3p0Helper.getDataBaseName(1));
		PreparedStatement countPs = this.find(toConn, toTable, null, COUNT);
		ResultSet countRs = this.getResultSet(countPs);
		try {
			if(countRs.next()){
				if(countRs.getInt(1)>0){//目标数据表不空
					if(operateBean.isDeleteSync()){//需要执行同步删除
						//找出目标表所有记录的主键
						Connection conn = c3p0Helper.getConnection(c3p0Helper.getDataBaseName(1));
						PreparedStatement dsPs = this.find(conn, toTable, null, operateBean.getIdColumns());
						ResultSet dsRs = this.getResultSet(dsPs);
						//conn ps rs需要放到线程中用完了再关闭
						new Thread(new DeleteSyncThread(conn, dsPs, dsRs, fromTable, toTable, operateBean.getIdColumns())).start();
					}
				}else{//目标数据表是空的，只需要新增
					operateBean.setOperate(0);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		Connection fromConn = c3p0Helper.getConnection(c3p0Helper.getDataBaseName(0));
		
		//查找oracle数据库中fromTable表数据
		PreparedStatement ps = this.find(fromConn, fromTable, initQueryTermBean, selectColumns);
		ResultSet rs = this.getResultSet(ps);
		
		if(operateBean.getOperate()==0){//全部是新增记录
			result = this.save(toConn, toTable, rs, null);
		}else if(operateBean.getOperate()==1){//全部是更新记录
			result = this.update(toConn, toTable, operateBean.getIdColumns(), rs, null);
		}else{//operateBean.getIdColumns()存在的记录做修改，其它记录新增
			result = this.saveOrUpdate(toConn, toTable, operateBean.getIdColumns(), rs, null);
		}

		c3p0Helper.closes(toConn);
		c3p0Helper.closes(fromConn, ps, rs);
		
		if(result){
			LOG.info("-----table ("+fromTable+"->"+toTable+") synchronized success-----");
		}else{
			LOG.info("-----table ("+fromTable+"->"+toTable+") Synchronous failure-----");
		}
	}

}
